home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekikoh Dennoh Club 5
/
Gekikoh Dennoh Club Vol. 5 (Japan).7z
/
Gekikoh Dennoh Club Vol. 5 (Japan) (Track 01).bin
/
internet
/
tcppack
/
tcppackb.lzh
/
src
/
libnetwork
/
arp.c
next >
Wrap
C/C++ Source or Header
|
1994-05-16
|
4KB
|
204 lines
/*
* arp.c
*
* Copyright (C) 1994 First Class Technology.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"tcpipdrv.h"
#include"network.h"
#define ARPSIZE 17
/* tcpipdrv é╠ arp.h é╠Ä└æòé╔ê╦æ╢ */
typedef struct _arp_table
{
struct _arp_table *prev;
struct _arp_table *next;
long tmp1;
long tmp2;
int state;
long tmp3;
long tmp4;
long ip_addr;
int hw_addr_len;
char hw_addr[16];
} _arp_table;
/*
* LIFE
*/
#define ARPLIFE (600) /* Lifetime of a valid ARP entry: 10 minutes */
#define ARP_RESEND (1) /* resend if no reply in 1 sec */
#define ARP_MAXRETRY (4)
/*
*
*/
#define ARP_PENDING (0)
#define ARP_VALID (1)
_ti_func search_ti_entry (void);
/************************************************
* *
************************************************/
char *
search_arp_table (long ipaddr, int len, char *dst)
{
_ti_func func = search_ti_entry ();
if (func)
{
_arp_table *cur;
cur = (_arp_table *)func (_TI_search_arp_table, (long*)ipaddr);
if (cur)
{
int bufsize;
bufsize = cur->hw_addr_len * 2 + (cur->hw_addr_len - 1) + 1;
if (cur->state == ARP_VALID)
{
if (bufsize < len)
{
int i;
char *p;
char *q;
p = dst;
q = cur->hw_addr;
for (i = 0; i < cur->hw_addr_len; i++)
{
sprintf (p, "%2.2x:", (unsigned char)*q++);
p += 3;
}
if (i)
*(p - 1) = '\0';
return dst;
}
}
else
{
if (len > 12)
{
strcpy (dst, "<incomplete>");
return dst;
}
}
}
}
return NULL;
}
/************************************************
* *
************************************************/
void
delete_arp_table (long ipaddr)
{
_ti_func func = search_ti_entry ();
if (func)
func (_TI_del_arp_table, (long*)ipaddr);
return;
}
/************************************************
* *
************************************************/
void
add_arp_table (long ipaddr, char *src)
{
_ti_func func = search_ti_entry ();
if (func)
{
long arg[2];
arg[0] = ipaddr;
arg[1] = (long)src;
func (_TI_add_arp_table, arg);
}
return;
}
/************************************************
* *
************************************************/
char *
chkhost (long target, int len, char *buff)
{
clock_t start, now;
_ti_func func = search_ti_entry ();
delete_arp_table (target);
if (func)
{
now = start = clock ();
/* request */
func (_TI_arp_request, (long *)target);
while ((now - start) / CLOCKS_PER_SEC < ARP_RESEND * (ARP_MAXRETRY))
{
search_arp_table (target, len, buff);
if (strcmp (buff, "<incomplete>"))
{
return buff;
}
now = clock ();
}
}
return NULL;
}
/************************************************
* *
************************************************/
long *
get_arp_array (int *len)
{
_ti_func func = search_ti_entry ();
_arp_table **_top, *_cur;
long *array;
int n, nn;
int x;
*len = 0;
array = NULL;
if (func)
{
_top = (_arp_table **)func (_TI_get_arp_table_top, NULL);
n = 0;
for (x = 0; x < ARPSIZE; x++)
{
for (_cur = _top[x]; _cur != NULL; _cur = _cur->next)
n++;
}
if (n)
{
array = (long *)malloc (sizeof (long) * n);
nn = 0;
for (x = 0; x < ARPSIZE; x++)
{
for (_cur = _top[x]; nn < n && _cur != NULL; nn++, _cur = _cur->next)
array[nn] = _cur->ip_addr;
}
*len = nn;
if (!nn)
{
free (array);
array = NULL;
}
}
}
return array;
}